package com.frxuexi.video

import com.frxuexi.lib_base.http.RequestType
import com.frxuexi.lib_base.http.VideoApiService
import com.frxuexi.lib_base.mvi.BaseViewModel
import com.frxuexi.lib_base.room.ZanDao
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.flow
import javax.inject.Inject

@HiltViewModel
class VideoViewModel @Inject constructor(val apiService: VideoApiService,val zanDao: ZanDao):BaseViewModel<VideoIntent>(){
    override fun handleIntent(it: VideoIntent) {
        when(it){
            is VideoIntent.getVideos->httpRequest(apiService.getVideos(it.page))
            is VideoIntent.isZan->roomRequest(flow { emit(zanDao.isZan(it.id)) },RequestType.SELECT_ITEM)
            is VideoIntent.addZan->roomRequest(flow { emit(zanDao.addZan(it.zanEntity)) },RequestType.ADD)
            is VideoIntent.cancelZan->roomRequest(flow { emit(zanDao.cancelZan(it.zanEntity)) },RequestType.DELETE)
            is VideoIntent.getComments->httpRequest(apiService.getComments(it.id),RequestType.COMMENTS)
            is VideoIntent.addComment->httpRequest(apiService.addComment(toBody(it.item)),RequestType.ADD_COMMENT)
            is VideoIntent.getFocuse->httpRequest(apiService.getFocuse(it.name),RequestType.GET_FOCUSE)
            is VideoIntent.addFocuse->httpRequest(apiService.addFocuse(it.focusUserName,it.focusedUserName),RequestType.ADD_FOCUSE)
            is VideoIntent.deleteFocuse->httpRequest(apiService.deleteFocuse(it.focusUserName,it.focusedUserName),RequestType.DELETE_FOCUSE)
        }
    }
}